﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

using System;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace TheOliver.Phone.BMI
{
    public class BMIDataContext : DataContext
    {
        public BMIDataContext() : 
            this("Data Source=isostore:/Database.sdf")
        {
            if (!this.DatabaseExists())
            {
                this.CreateDatabase();
            }
        }

        public BMIDataContext(string connectionString)
            : base(connectionString)
        {

        }

        public Table<BMIItem> Items;

        public void AddBmi(double bmiValue)
        {
            BMIItem newBmi = new BMIItem();
            newBmi.Bmi = bmiValue;
            newBmi.Date = DateTime.Now;
            this.Items.InsertOnSubmit(newBmi);

            this.SubmitChanges();
        }

        internal void DeleteBmi(BMIItem item)
        {
            this.Items.DeleteOnSubmit(item);
            this.SubmitChanges();
        }
    }

    [Table]
    public class BMIItem 
    {
        private int _id;
        
        [Column(IsPrimaryKey=true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull= false, AutoSync=AutoSync.OnInsert)]
        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        private DateTime _date;

        [Column]
        public DateTime Date
        {
            get { return _date; }
            set { _date = value; }
        }

        private double _bmi;

        [Column]
        public double Bmi
        {
            get { return _bmi; }
            set { _bmi = value; }
        }        
    }
}
